package com.example.androiddemo01.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.example.androiddemo01.R;
import com.example.androiddemo01.utils.SpUtils;
import com.example.androiddemo01.widget.HeaderView;

public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {

   private HeaderView mHeaderView;
   private Button mBtnLogout;
   private SpUtils mSpUtils;
   private SwitchCompat mAdminSwitch;
   private RelativeLayout mRlAddStudent;
   private RelativeLayout mRlAddCourse;
   private RelativeLayout mRlChPassword;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_settings);
      mHeaderView = findViewById(R.id.header_view);
      mHeaderView.setRightVisibility(View.GONE);
      mHeaderView.setTitle("设置");

      mAdminSwitch = findViewById(R.id.admin_switch);
      mAdminSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            showAdminViews(isChecked);
         }
      });
      mRlAddStudent = findViewById(R.id.add_student_view);
      mRlAddStudent.setOnClickListener(this);

      mRlAddCourse = findViewById(R.id.add_course_view);
      mRlAddCourse.setOnClickListener(this);

      mRlChPassword = findViewById(R.id.add_change_password);
      mRlChPassword.setOnClickListener(this);

      mBtnLogout = findViewById(R.id.logout_button);
      mBtnLogout.setOnClickListener(this);
      mSpUtils = new SpUtils(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.logout_button) { //退出登录
         mSpUtils.putInt(SpUtils.LOGIN_KEY, -1);
         Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
      } else if (id == R.id.add_student_view) { //跳转添加学生界面
         Intent intent = new Intent(SettingsActivity.this, AddStudentActivity.class);
         startActivity(intent);
      }else if (id == R.id.add_course_view) { //跳转添加课程界面
         Intent intent = new Intent(SettingsActivity.this, AddCourseActivity.class);
         startActivity(intent);

      }else if (id == R.id.add_change_password) { //跳转修改密码界面
         Intent intent = new Intent(SettingsActivity.this, ChangePasswordActivity.class);
         startActivity(intent);
      }
   }

   private void showAdminViews(boolean show) {
      View view = findViewById(R.id.admin_views);
      view.setVisibility(show ? View.VISIBLE : View.GONE);
   }
}
